/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.model;

import java.io.IOException;

import javax.swing.JProgressBar;

import soccerscope.util.Time;

public class SceneSetMaker extends Thread {

	private SceneBuilder builder;

	private SceneSet sceneSet;

	private JProgressBar progress = null;


        public SceneSetMaker(SceneBuilder builder, SceneSet sceneSet) {
            this.builder = builder;
            this.sceneSet = sceneSet;
        }

	public SceneSetMaker(SceneBuilder builder, SceneSet sceneSet,
			JProgressBar progress) {
		this.builder = builder;
		this.sceneSet = sceneSet;
		this.progress = progress;
	}

	@Override
  public void run() {

		try {
			setName("DAI-NAMITE SoccerScope SceneSetMaker");
		} catch (SecurityException e) {
			System.out.println("Could not set the thread name");
		}
		ClientScene clientScene = this.sceneSet.lastScene();
		Team left = clientScene.left;
		Team right = clientScene.right;
		PlayMode playmode = clientScene.pmode;
		byte[] packet = new byte[4096];
		if (this.progress != null) {
			this.progress.setMaximum(Time.GAME_TIME);
			this.progress.setMinimum(0);
			this.progress.setValue(0);
		}
		System.out.println();
		while (true) {
			int size;
			try {
				size = this.builder.readPacket(packet);
				if (size == -1) {
					this.sceneSet.analyze();
					if (this.progress != null)
						this.progress.setValue(0);
					// Toolkit.getDefaultToolkit().beep();
					return;
				}
			} catch (IOException ie) {
				System.err.println(ie);
				return;
			}

			switch (this.builder.getPacketType(packet)) {
			case SceneBuilder.SHOW_MODE:
				// here, a client-scene is created during game,
				clientScene = this.builder.makeScene(packet, playmode, left, right);
				this.sceneSet.addScene(clientScene);

				if (WorldModel.getInstance().isTrainingMode()) {
					WorldModel.getInstance().executeTraining();
				}

				if (this.progress != null)
					this.progress.setValue(clientScene.time);
				break;

			case SceneBuilder.MSG_MODE:
				// System.out.println("MSG_MODE");
				this.builder
						.addMessage(clientScene,
								new String(packet, 6, size - 7));
				break;

			case SceneBuilder.DRAW_MODE:
				// System.out.println("DRAW_MODE");
				break;

			case SceneBuilder.BLANK_MODE:
				// System.out.println("BLANK_MODE");
				break;

			case SceneBuilder.PM_MODE:
				// System.out.println("PM_MODE");
				playmode = this.builder.makePlayMode(packet);
				break;

			case SceneBuilder.TEAM_MODE:
				// System.out.println("TEAM_MODE");
				left = this.builder.makeLeftTeam(packet, 2);
				right = this.builder.makeRightTeam(packet, 2);
				break;

			case SceneBuilder.PT_MODE:
				// System.out.println("PT_MODE");
				this.builder.makeHeteroParam(packet);
				break;

			case SceneBuilder.PARAM_MODE:
				// System.out.println("PARAM_MODE");
				this.builder.printServerParameter(packet);
				break;

			case SceneBuilder.PPARAM_MODE:
				// System.out.println("PPARAM_MODE");
				this.builder.printPlayerParameter(packet);
				break;
			}
		}
	}
}
